package baidu.hudson.plugin.result.collector;

import baidu.hudson.plugin.result.collector.utils.LocalCache;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Locale;

/**
 * there is the class which is used to receive the config data ,
 * and also contain some verfication methods.
 * User: yuxinlei
 * Date: 4/12/11
 * Time: 11:27 AM
 */
@Extension(ordinal = 1000.0)
public class ResultDescriptor extends BuildStepDescriptor<Publisher> {
    private static String DEFAULT_ENDPOINT = "tcp://db-testing-atd00.db01.baidu.com:8230";//"tcp://127.0.0.1:62626";//;
    private String remoteEndPoint;

    /**
     * Instantiates a new descriptor.
     */
    public ResultDescriptor() {
        super(ResultPublisher.class);
    }

    /**
     * @inheritDoc
     */
    @Override
    public boolean isApplicable(Class<? extends AbstractProject> jobType) {
        return true;
    }

    /**
     * @inheritDoc
     */
    @Override
    public String getDisplayName() {
        return Messages.Result_collector_name();
    }

    /**
     * @inheritDoc
     */
    @Override
    public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
        setRemoteEndPoint(formData.getString("remoteEndPoint"));
        save();
        return super.configure(req, formData);
    }

    /**
     * @param req
     * @param formData
     * @return
     * @throws FormException
     * @inheritDoc <br>for this extends , we add some proeprties for the data abstract , we get those
     * properties from the enviroment instead of user input
     */
    @Override
    public Publisher newInstance(StaplerRequest req, JSONObject formData) throws FormException {
        LocalCache.getInstance().setServerName(req.getServerName());
        LocalCache.getInstance().setServerLocalName(req.getLocalName());
        LocalCache.getInstance().setServerPort(String.valueOf(req.getServerPort()));
        LocalCache.getInstance().setServerLocalAddr(req.getLocalAddr());
        Hudson.getInstance().getRootUrl();
        Hudson.getInstance().getRootUrlFromRequest();
        formData.put("hudsonUrl", Hudson.getInstance().getRootUrlFromRequest());
        formData.put("localServername", req.getServerName());
        // add by yanshuai
        String[] segments = req.getOriginalRequestURI().split("/");
        if (segments.length >= 2)
        {
            formData.put("urlJobname", segments[segments.length - 2]);
        }
        else
        {
            formData.put("urlJobname", "unknow");
        }
        // add by yanshuai
        return super.newInstance(req, formData);
    }



    /**
         * Performs on-the-fly validation of the form field 'icafeModule'.
         *
         * @param value
         *      This parameter receives the value that the user has typed.
         * @return
         *      Indicates the outcome of the validation. This is sent to the browser.
         */
        public FormValidation doCheckIcafeModule(@QueryParameter String value) throws IOException, ServletException {
            if(value.length()==0)
                return FormValidation.error("Please set a name");
            if(value.length()<4)
                return FormValidation.warning("Isn't the name too short?");
            return FormValidation.ok();
        }


        /**
         * Performs on-the-fly validation of the form field 'icafeProject'.
         *
         * @param value
         *      This parameter receives the value that the user has typed.
         * @return
         *      Indicates the outcome of the validation. This is sent to the browser.
         */
        public FormValidation doCheckIcafeProject(@QueryParameter String value) throws IOException, ServletException {
            if(value.length()==0)
                return FormValidation.error("Please set a name");
            if(value.length()<4)
                return FormValidation.warning("Isn't the name too short?");
            return FormValidation.ok();
        }


    public String getRemoteEndPoint() {
        if (remoteEndPoint == null || remoteEndPoint.length() == 0) {
            setRemoteEndPoint(DEFAULT_ENDPOINT);
        }
        return remoteEndPoint;
    }

    public void setRemoteEndPoint(String remoteEndPoint) {
        this.remoteEndPoint = remoteEndPoint;
    }
}
